#!/bin/bash
# 简化版编译脚本，直接使用工具的完整路径

# 设置工具完整路径
MAKE="/c/Program Files (x86)/GnuWin32/bin/make.exe"
ARM_GCC="/d/arm-gnu-toolchain-14.3.rel1-mingw-w64-i686-arm-none-eabi/bin/arm-none-eabi-gcc.exe"
ARM_GXX="/d/arm-gnu-toolchain-14.3.rel1-mingw-w64-i686-arm-none-eabi/bin/arm-none-eabi-g++.exe"
ARM_AS="/d/arm-gnu-toolchain-14.3.rel1-mingw-w64-i686-arm-none-eabi/bin/arm-none-eabi-gcc.exe"
ARM_OBJCOPY="/d/arm-gnu-toolchain-14.3.rel1-mingw-w64-i686-arm-none-eabi/bin/arm-none-eabi-objcopy.exe"
ARM_AR="/d/arm-gnu-toolchain-14.3.rel1-mingw-w64-i686-arm-none-eabi/bin/arm-none-eabi-ar.exe"
ARM_SIZE="/d/arm-gnu-toolchain-14.3.rel1-mingw-w64-i686-arm-none-eabi/bin/arm-none-eabi-size.exe"

# 验证make是否可用
echo "验证编译工具..."
if [ ! -f "$MAKE" ]; then
    echo "错误: 未找到make工具，请检查路径是否正确: $MAKE"
    echo "建议从 http://gnuwin32.sourceforge.net/packages/make.htm 下载安装"
    exit 1
fi

if [ ! -f "$ARM_GCC" ]; then
    echo "错误: 未找到ARM工具链，请检查路径是否正确: $ARM_GCC"
    exit 1
fi

# 输出工具版本信息
echo "make已找到: $MAKE"
echo "ARM工具链已找到: $ARM_GCC"

# 创建build目录（如果不存在）
if [ ! -d "build" ]; then
    mkdir -p build
fi

# 使用完整路径执行make，并传递工具链变量
echo -e "\n开始编译ODrive固件..."
$MAKE CC="$ARM_GCC" CXX="$ARM_GXX" AS="$ARM_AS -x assembler-with-cpp" CP="$ARM_OBJCOPY" AR="$ARM_AR" SZ="$ARM_SIZE" -j4

# 检查编译结果
if [ $? -eq 0 ]; then
    echo -e "\n编译成功!"
    echo "输出文件位于build目录"
else
    echo -e "\n编译失败，请查看上面的错误信息"
fi